package com.zmht.beike.biz.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zmht.beike.biz.CMonthContentBiz;
import com.zmht.beike.core.BaseBizImpl;
import com.zmht.beike.po.TCmonthcontent;

public class CMonthContentBizImpl extends BaseBizImpl implements
		CMonthContentBiz {
	/*
	 * 删除记录
	 * (non-Javadoc)
	 * @see com.zmht.beike.biz.CMonthContentBiz#delCmontContent(java.io.Serializable)
	 */
	public boolean delCmontContent(Serializable id) {
		Object obj = this.getCmonthContentDAO().findById(TCmonthcontent.class, id);
		if(obj!=null){
			try{
				this.getCmonthContentDAO().deleteObj(obj);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return true;
	}
	/*
	 * 查询月主题内容信息
	 * (non-Javadoc)
	 * @see com.zmht.beike.biz.CMonthContentBiz#findCmonthContent(java.lang.String, java.lang.Integer, int, int)
	 */
	public List findCmonthContent(String sessionId, Integer teacherId,int month,
			int startIndex, int pageSize) {
		String hql = "from TCmonthcontent t where t.sessionID='"+sessionId+"' and t.teacherId="+teacherId+" and t.cmonthId=null and t.month="+month;
		return this.getCmonthContentDAO().findByPage(hql, startIndex, pageSize);
		
	}
	
	/*
	 * 查询月主题信息条数
	 * (non-Javadoc)
	 * @see com.zmht.beike.biz.CMonthContentBiz#findCmonthContentTotal(java.lang.String, java.lang.Integer, int, int)
	 */
	public int findCmonthContentTotal(String sessionId, Integer teacherId,int month,
			int startIndex, int pageSize) {
		String hql = "from TCmonthcontent t where t.sessionID='"+sessionId+"' and t.teacherId="+teacherId+" and t.cmonthId=null and t.month="+month;
		return this.getCmonthContentDAO().findByHQLObj(hql).size();//.findByHql(hql).size();
		
	}

	public boolean saveCmontContent(TCmonthcontent monthContent) {
		try{
			this.getCmonthContentDAO().saveObj(monthContent);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	public boolean updateCmontContent(TCmonthcontent monthContent) {
		try{
			this.getCmonthContentDAO().updateObj(monthContent);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	public static void main(String [] args){
		ApplicationContext cxt;
		cxt = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring_dao.xml","spring/spring_beike.xml",});
	
		CMonthContentBizImpl biz  = (CMonthContentBizImpl)cxt.getBean("cmonthContentBizTag");
		System.out.println(biz);
		//保存记录
		/*TCmonthcontent monthContent = new TCmonthcontent();
		monthContent.setMonth(2);//设置月份
		monthContent.setSessionID("1111");
		monthContent.setMonthThemeName("月主题");
		
		biz.saveCmontContent(monthContent);
		*/
		//查询记录
		/*	List list = biz.findCmonthContent("1111", 1,1,0, 100);
			System.out.println(list.size());
		 */		
		
		
	}
	public TCmonthcontent findById(Serializable id) {
		
		return (TCmonthcontent)this.getCmonthContentDAO().findById(TCmonthcontent.class, id);
	}
	/**
	 * 查询记录
	 */
	public List<TCmonthcontent> findCmonthContentEdit(Integer targetId,
			Integer teacherId,Integer areaId, int month, int startIndex, int pageSize) {
		String hql = "from TCmonthcontent t where t.caimId="+targetId+" and t.teacherId="+teacherId+" and t.month="+month;
		if(areaId!=null&&!new Integer(0).equals(areaId)){
			hql += " and t.careaId="+areaId;
		}
		return this.getCmonthContentDAO().findByPage(hql, startIndex, pageSize);
	}
	/**
	 * 查询记录总条数
	 */
	public int findCmonthContentEditTotal(Integer targetId, Integer teacherId,Integer areaId,
			int month) {
		String hql = "from TCmonthcontent t where t.caimId="+targetId+" and t.teacherId="+teacherId+" and t.month="+month;
		if(areaId!=null&&!new Integer(0).equals(areaId)){
			hql += " and t.careaId="+areaId;
		}
		return this.getCmonthContentDAO().findByPageAmount(hql);
	}
	
}
